package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.model.common.AuthParam;
import com.cdks.wuxiu.api.model.dto.InviteStatisDTO;
import com.cdks.wuxiu.api.model.param.ShareInviterBindParam;
import com.cdks.wuxiu.api.model.param.ShareGetRecordParam;
import com.cdks.wuxiu.api.service.CallService;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "分享")
@Slf4j
@RestController
@RequestMapping("/share")
public class ShareController {
    @Resource
    private UserService userService;
    @Resource
    private CallService callService;
        

    @ApiOperation("首页")
    @PostMapping("/home")
    public ApiResult<Object> home(@RequestBody @Valid AuthParam param) {
        if (param.getUserInfo().getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            return ApiResult.failed(10, "无权限");
        }

        UserBaseInfo inviter  = userService.getInviterInfo(param.getUserId());
        InviteStatisDTO statis  = userService.getInviteStatis(param.getUserId());

        List<String> rule  = new ArrayList<>();
        if (param.getBranch() > 199)
        {
            rule.add("1、被邀请用户注册登录后即自动绑定邀请关系");
            rule.add("2、如果出现漏绑请于24小时内联系客服补绑");
            rule.add("3、若有疑问请咨询客服");
            rule.add("");    
        }
        else
        {
            rule.add("1、成功邀请后，以下情况您将获得积分奖励。积分可消费也可提现（无论您是否认证）");
            rule.add(" ● 您邀请的用户成功发布心情或评论别人的心情，每次您都将获得积分奖励");
            rule.add(" ● 您邀请的用户拨打或接听在线通话，您都将根据通话费用按比例获得积分奖励");
            rule.add(" ● 您邀请的用户成功充值，您将获得充值心豆20%的积分奖励");
            rule.add("2、被邀请用户注册登录后即自动绑定邀请关系");
            rule.add("3、如果出现漏绑请于24小时内联系客服补绑");
            rule.add("");
        }

        JSONObject json  = new JSONObject();
        json.set("inviteCode", param.getUserId());
        json.set("inviter", inviter);
        json.set("rule", rule);
        json.set("totalCount", statis.getCount());
        json.set("totalScore", statis.getScore());

        return ApiResult.success(json);
    }
    
    @ApiOperation("自动绑定邀请人")
    @PostMapping("/bindInviter")
    public ApiResult<Object> bindInviter(@RequestBody @Valid ShareInviterBindParam param) {
        if (param.getUserId().equals(param.getInviter()))
        {
            return ApiResult.failed(10, "邀请人信息错误");
        }

        int ret  = userService.bindInviter(param.getUserInfo(), param.getInviter());
        if (ret < 0)
        {
            return ApiResult.failed(10, "绑定失败");
        }

        return ApiResult.success();
    }
    
    @ApiOperation("手动绑定邀请人")
    @PostMapping("/updateInviter")
    public ApiResult<Object> updateInviter(@RequestBody @Valid ShareInviterBindParam param) {
        if (param.getUserId().equals(param.getInviter()))
        {
            return ApiResult.failed(10, "邀请人信息错误");
        }

        int ret  = userService.updateInviter(param.getUserInfo(), param.getInviter());
        if (ret < 0)
        {
            return ApiResult.failed(10, "绑定失败");
        }

        return ApiResult.success();
    }
    
    @ApiOperation("获取邀请记录")
    @PostMapping("/getRecord")
    public ApiResult<Object> getRecord(@RequestBody @Valid ShareGetRecordParam param) {
        JSONObject json  = new JSONObject();
        json.set("inviteList", userService.getInviteList(param.getUserId(), param.getPage(), param.getStart(), param.getStop()));

        return ApiResult.success(json);
    }
}
